package esmp.jtt808.command;

import esmp.jtt808.model.DataPack;
import esmp.jtt808.model.platform.SetTerminalParam;
import io.netty.buffer.Unpooled;
import netty.session.NettySession;
import netty.session.SessionManager;
import netty.task.RemoteCmd;
import netty.task.invoker.RemoteCmdInvoker;

/**
 * Created by dave on 18-7-20 下午8:04.
 */
public class SetTerminalParamCmd implements RemoteCmdInvoker {
    private SessionManager sessionManager;

    public SetTerminalParamCmd(SessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    @Override
    public void invoke(RemoteCmd cmd) {
        NettySession session = sessionManager.getSession(cmd.getSessionId());
        DataPack<SetTerminalParam> dp = new DataPack<>();

        dp.setMsgId(Jtt808Cmd.setParam.getCode());
        dp.setSim(session.getSessionId());
        dp.setSequenceNo(session.nextSequenceNo());
        dp.setBody((SetTerminalParam) cmd.getParam());
        session.getChannel().writeAndFlush(Unpooled.wrappedBuffer(dp.encode()));
    }

    @Override
    public boolean support(RemoteCmd cmd) {
        return Jtt808Cmd.setParam.name().equals(cmd.getCmdType());
    }
}
